package com.songtech.uitest.module.common;

import com.songtech.uitest.util.AutoWait;
import com.songtech.uitest.util.Space;
import org.openqa.selenium.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MenuHandler {

    private Logger logger = LoggerFactory.getLogger(MenuHandler.class);

    //打印日志前的缩进空格
    @Autowired
    private Space space;

    public void clickMenuItem(WebDriver driver, String mainItemName, String subItemName, String subsubItemName) throws Exception{
        //主菜单路径：            /html/body/div[1]/div[6]/aside/ul/li[3]/a/span
//一级子菜单路径：                /html/body/div[1]/div[6]/aside/ul/li[3]/ul/li[1]/a/span

//一级子菜单路径：                /html/body/div[1]/div[6]/aside/ul/li[5]/ul/li[5]/a/span
//二级子菜单路径                  /html/body/div[1]/div[6]/aside/ul/li[5]/ul/li[5]/ul/li[3]/a/span

        driver.switchTo().defaultContent();

//        WebElement li = driver.findElement(By.xpath("//aside/ul//li/a/span[text()='"+ mainItemName +"']/../.."));
//        WebElement a = li.findElement(By.xpath(".//ul//li/a/span[text()='"+ subItemName +"']/.."));
//
//        scrollAndClick(driver, li);
//        Thread.sleep(1000);
//        scrollAndClick(driver, a);
//        Thread.sleep(1000);

        AutoWait.setWebDriverWait(driver);

        WebElement li = AutoWait.scrollAndFindByXpath("//aside/ul//li/a/span[text()='"+ mainItemName +"']/../..");
        li.click();
        WebElement a = AutoWait.scrollAndFindByXpath(li, ".//ul//li/a/span[text()='"+ subItemName +"']/..");
        a.click();

        if(!subsubItemName.equals("")){
//            WebElement aa = a.findElement(By.xpath("..//ul//li/a/span[text()='"+ subsubItemName +"']/.."));
//            scrollAndClick(driver, aa);
            AutoWait.scrollAndFindByXpath(a, "..//ul//li/a/span[text()='"+ subsubItemName +"']/..").click();
            logger.info(space.getSpace() + "打开" + mainItemName + "=>" + subItemName + "=>" + subsubItemName +"页面");
        }
        else {
            logger.info(space.getSpace() + "打开" + mainItemName + "=>" + subItemName + "页面");
        }

        Thread.sleep(6000);//////////////////////////////////////////////////等待去掉

    }


//    public void scrollAndClick(WebDriver driver, WebElement element) throws Exception{
//        //拖动滚动条到元素能出现的地方
//        String js = "arguments[0].scrollIntoView(true);";
//        ((JavascriptExecutor) driver).executeScript(js, element);
//        element.click();
//    }

}
